//
//  ThroughBusLineViewModel.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/15.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "ThroughBusLineViewModel.h"
#import "NSDate+WQCalendarLogic.h"
#import "KDateFormatter.h"
@implementation ThroughBusLineViewModel

-(id)init{
    self.throughBusLineList = [[NSMutableArray alloc ] init];
    self.requestThroughBusLineEntity = [[RequestThroughBusLineEntity alloc ] init];
    return [super init];
}

/**
 *  加载直通车线路列表
 *
 *  @param resultBlock
 */
-(void)loadThroughBusLineList:(DealDataResultBlock)resultBlock{
    
    self.requestThroughBusLineEntity.PageIndex = @(self.pageIndex).stringValue;
    WEAKSELF
    [[BaseHttpRequest sharedInstance] sendRequestToPost:ThroughList withData:self.requestThroughBusLineEntity alertMessage:@"" success:^(NSDictionary *dicData) {
        if (weakSelf.pageIndex == 1) {
            [weakSelf.throughBusLineList removeAllObjects];
        }
        NSArray *list = [dicData objectForKey:@"starightTratin"];
        for (NSDictionary *dic in list) {
            ResponseThroughBusLineEntity * entity = [[ResponseThroughBusLineEntity alloc ] initWithDictionary:dic];
            [weakSelf.throughBusLineList addObject:entity];
        }
        
        if (list.count == 0) {
           resultBlock(3,nil,nil);
        }else{
            resultBlock(1,nil,nil);
        }
        
        
    
    } error:^(NSString *errorMsg) {
        resultBlock(1,nil,errorMsg);
    }];
}

/**
 *  获取直通车线路日期
 *
 *  @return
 */
-(NSMutableArray *)getThroughBusLineDateList{
    NSMutableArray *array = [NSMutableArray array];
    
    NSDate *date = [NSDate date];
    //获取当前月有几天
    NSInteger currentMDays = [date numberOfDaysInCurrentMonth];
    
    //获取当前日
    NSInteger today = [date getCurrentDay];
    
    //当月的有效日期
    for (NSInteger i = 0; i <= currentMDays - today; i++) {
        ResponseDayModel *model = [[ResponseDayModel alloc] init];
        NSDate *futureDate = [date dayInTheFollowingDay:i];
        model.time = [KDateFormatter dateToStringDate:futureDate formatString:KDateFormatterStyle4];
        model.price = @"";
        [array addObject:model];
    }
    
    for (NSInteger j= 1; j <= 5; j++) {
        //获取下一个月的第一天
        NSDate *nestDate = [date dayInTheFollowingMonth:j];
        NSDate *firstDate = [nestDate firstDayOfCurrentMonth];
        //获取下一个月的天数
        NSInteger nestMDays = [firstDate numberOfDaysInCurrentMonth];
        
        //下月的有效日期
        for (NSInteger i = 0; i < nestMDays; i++) {
            ResponseDayModel *model = [[ResponseDayModel alloc] init];
            
            model.price = @"";
            NSDate *futureDate = [firstDate dayInTheFollowingDay:i];
            model.time = [KDateFormatter dateToStringDate:futureDate formatString:KDateFormatterStyle4];
            [array addObject:model];
        }
        
    }
    
  
    
    self.showMaxDateString = [[array lastObject] time];
    return array;

}
@end
